
package com.yimixian.app.address;

import com.yimixian.app.R;
import com.yimixian.app.model.Address;
import com.yimixian.app.util.CustomViewUtils;
import com.yimixian.app.util.Strings;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class AddressView extends FrameLayout {

    @InjectView(R.id.name_text)
    TextView mNameText;
    @InjectView(R.id.address_text)
    TextView mAddressText;
    @InjectView(R.id.selected_band)
    View mSelectedBand;
    @InjectView(R.id.bottom_divider)
    View mBottomDivider;
    @InjectView(R.id.edit_icon)
    ImageView mEditIcon;
    @InjectView(R.id.tv_tag)
    TextView mTvTag;

    private Address mAddress;

    public AddressView(Context context) {
        super(context);
        initAddressView();
    }

    public AddressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAddressView();
    }

    public AddressView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initAddressView();
    }

    private void initAddressView() {
        LayoutInflater.from(getContext()).inflate(R.layout.address_view, this);
        ButterKnife.inject(this);
        CustomViewUtils.setViewBackground(this,
                getResources().getDrawable(R.drawable.settings_item_bg));
        setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                getResources().getDimensionPixelSize(R.dimen.address_item_height)));
    }

    public void bind(Address address) {
        mAddress = address;
        StringBuilder nameBuilder = new StringBuilder(address.name);
        if (!Strings.isNullOrEmpty(address.gender)) {
            nameBuilder.append(" (" + address.gender + ")");
        }
        nameBuilder.append(Strings.repeat(" ", 4));
        if (!Strings.isNullOrEmpty(address.tel)) {
            nameBuilder.append(address.tel);
        }
        mNameText.setText(nameBuilder);
        mEditIcon.setTag(address.id);
        if (TextUtils.isEmpty(address.tag)) {
            mTvTag.setVisibility(View.GONE);
        }else {
            mTvTag.setVisibility(View.VISIBLE);
            mTvTag.setText(address.tag);
        }
        boolean hasPoi = !Strings.isNullOrEmpty(address.poiId);
        //经纬度返回-1无poi信息，为手动填写地址
        if("-1".equals(address.latitude) && "-1".equals(address.longitude)){
            mAddressText.setText(address.poiAddress + " " + Strings.nullToEmpty(mAddress.roomNo));
        }else{
            mAddressText.setText(hasPoi ? address.poiName + " " + Strings.nullToEmpty(mAddress.roomNo)
                    : "[请补全当前地址]");
        }
        if("-1".equals(address.latitude) && "-1".equals(address.longitude)){
            mAddressText.setTextColor(0xff333333);
        }else{
            mAddressText
                    .setTextColor(hasPoi ? 0xff333333 : getResources().getColor(R.color.ymx_orange));
        }
    }

    public void setEditIconClickListener(OnClickListener listener) {
        mEditIcon.setOnClickListener(listener);
    }

    public Address getBoundAddress() {
        return mAddress;
    }

    public void setSelectedBandVisible(boolean visible) {
        mSelectedBand.setVisibility(visible ? VISIBLE : INVISIBLE);
    }

    public void setBottomDividerVisible(boolean visible) {
        mBottomDivider.setVisibility(visible ? VISIBLE : INVISIBLE);
    }
}
